package com.ruoyi.monitormanage.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.monitormanage.domain.TTestpoint;
import com.ruoyi.monitormanage.mapper.TTestpointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.monitormanage.domain.TReviewgps;
import com.ruoyi.monitormanage.service.ITReviewgpsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 生成审核pgsController
 * 
 * @author admin
 * @date 2025-10-22
 */
@RestController
@RequestMapping("/reviewgps")
public class TReviewgpsController extends BaseController
{
    @Autowired
    private ITReviewgpsService tReviewgpsService;
    @Autowired
    private TTestpointMapper tTestpointMapper;
    /**
     * 查询生成审核pgs列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TReviewgps tReviewgps)
    {
        startPage();
        List<TReviewgps> list = tReviewgpsService.selectTReviewgpsList(tReviewgps);
        return getDataTable(list);
    }

    /**
     * 导出生成审核pgs列表
     */
    @RequiresPermissions("monitormanage:reviewgps:export")
    @Log(title = "生成审核pgs", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TReviewgps tReviewgps)
    {
        List<TReviewgps> list = tReviewgpsService.selectTReviewgpsList(tReviewgps);
        ExcelUtil<TReviewgps> util = new ExcelUtil<TReviewgps>(TReviewgps.class);
        util.exportExcel(response, list, "生成审核pgs数据");
    }

    /**
     * 获取生成审核pgs详细信息
     */
    @RequiresPermissions("monitormanage:reviewgps:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tReviewgpsService.selectTReviewgpsById(id));
    }

    /**
     * 新增生成审核pgs
     */
    @Log(title = "生成审核pgs", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TReviewgps tReviewgps)
    {
        return toAjax(tReviewgpsService.insertTReviewgps(tReviewgps));
    }

    /**
     * 修改生成审核pgs
     */
    @Log(title = "生成审核pgs", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody TReviewgps tReviewgps)
    {
        if (tReviewgpsService.updateTReviewgps(tReviewgps) > 0){
            if (tReviewgps.getStatus() == 1){//说明通过审核
                TTestpoint testpoint = tTestpointMapper.getTestpoint(tReviewgps.getYear(), tReviewgps.getDeptid());
                String gps = testpoint.getGps();
                if (gps != null && !gps.trim().isEmpty()) {
                    testpoint.setGps(testpoint.getGps() + ";" + tReviewgps.getGps());
                } else {
                    testpoint.setGps(tReviewgps.getGps());
                }
                testpoint.setStatus("1");
                tTestpointMapper.updateTTestpoint(testpoint);
                return AjaxResult.success("200","操作成功");
            } else if (tReviewgps.getStatus() == 2) {
                return AjaxResult.success("200","操作成功");
            }
        }
        return AjaxResult.error("500","操作失败，请联系管理员");
    }

    /**
     * 删除生成审核pgs
     */
    @RequiresPermissions("monitormanage:reviewgps:remove")
    @Log(title = "生成审核pgs", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tReviewgpsService.deleteTReviewgpsByIds(ids));
    }
}
